<template>
  <div class="contaniner">
    <div class="hearder">留言板</div>
    <el-input
      v-model="textarea"
      :autosize="{ minRows: 4, maxRows: 4 }"
      type="textarea"
      placeholder="快来给作者留言吧。。。"
      class="content"
    />
    <div v-for="(item) in worddata" :key="item.id" class="box">
      <div class="top">
        <div class="img">
          <img
            src="https://img1.baidu.com/it/u=4256350010,1509363888&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400"
          />
        </div>
        <div class="right">
          <div class="name">{{ item.username }}</div>
          <div class="date">{{ item.date }}</div>
        </div>
        <el-button @click="godelede(item)" text type="warning">删除</el-button>
      </div>
      <div class="center">
        {{ item.content }}
      </div>
    </div>
    <el-button type="warning" plain class="btn" @click="gofinish">发表</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { getworddata, deletworddata, addeworddata } from '../../api/word/index'
import type { reqtype,resgetword,wordlist} from '@/api/word/type.ts'
//引入用户相关的小仓库
import { useUserStore } from '@/stores/user'
let useStore = useUserStore()
const textarea = ref('')

const worddata = ref<wordlist[]>([])
onMounted(() => {
  initdata()
})

const initdata = async () => {
  worddata.value=[]
  let result:resgetword = await getworddata()
  if (result.status == 0) {
    for(let i=0;i<result.data.length;i++){
        if(result.data[i].username===useStore.username)
        {   
            console.log(result.data[i]);
            worddata.value.push(result.data[i])
        }
    }
  }
}

const formattedDate = computed(() => {
  const currentDate = new Date()
  const year = currentDate.getFullYear()
  const month = padNumber(currentDate.getMonth() + 1) // 月份从 0 开始，需要加 1
  const day = padNumber(currentDate.getDate())
  const hours = padNumber(currentDate.getHours())
  const minutes = padNumber(currentDate.getMinutes())
  const seconds = padNumber(currentDate.getSeconds())
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
})

const padNumber = (num:any) => {
  return num.toString().padStart(2, '0')
}


import { ElMessage } from 'element-plus'
const gofinish = async () => {
  const data = {
    username: useStore.username,
    date: formattedDate.value,
    content: textarea.value
  }
  let result:reqtype = await addeworddata(data)
  if (result.status == 0) {
    ElMessage({
      message: '发表成功！',
      type: 'success'
    })
    textarea.value = ''
    initdata()
  }
}
import { ElMessageBox } from 'element-plus'

const godelede = async (item:wordlist) => {
  ElMessageBox.confirm('确定删除该留言吗?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async() => {
      let result:reqtype = await deletworddata(item.id as number)
      if (result.status == 0) {
        ElMessage({
          message: '删除成功！',
          type: 'success'
        })
        initdata()
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除！'
      })
    })
}
</script>

<style scoped>
.contaniner {
  width: 100%;
  background-color: #e1eef6;
  border-radius: 10px;
  .hearder {
    padding: 20px;
    text-align: center;
    font-size: 25px;
  }
  .content {
    margin: 10px;
    border-radius: 5px;
    width: 80%;
    margin-left: 30px;
  }
  .btn {
    width: 80%;
    margin: 30px;
  }
  .box {
    margin-left: 30px;
    margin-right: 30px;
    margin-bottom: 15px;
    .top {
      display: flex;

      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
      .right {
        display: flex;
        justify-content: center;
        flex-direction: column;
        /* align-items: center; */
        margin-left: 15px;
        .name {
          font-size: 20px;
          color: #ffbc42;
        }
        .date {
          color: #666;
        }
      }
      
    }
    .center {
      color: #666;
      font-size: 14px;
      margin-top: 5px;
      height: auto;
    }
  }
}
</style>
